﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace QXBook.Service
{
    public class ConfigService
    {
        /// <summary>
        /// 初始化基本配置信息
        /// </summary>
        public static void BaseConfigInit()
        {
            //获得所有需要登陆的特殊页面
            XmlNodeList baseconfig = GetXmlNotListByTagNameAndFilePath(m_BaseConfigFilePath, "Item");

            _baseconfig = new Dictionary<string, string>();
            foreach (XmlElement element in baseconfig)
            {
                _baseconfig.Add(element.SelectSingleNode("Name").InnerText, element.SelectSingleNode("Value").InnerText);
            }

        }

        /// <summary>
        /// 获得XML 节点名相同的元素
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="tagname"></param>
        /// <returns></returns>
        private static XmlNodeList GetXmlNotListByTagNameAndFilePath(string filepath, string tagname)
        {
            XmlNodeList mylist = null;

            try
            {
                XmlDocument document1 = new XmlDocument();
                if (HttpContext.Current != null)
                {
                    document1.Load(HttpContext.Current.Server.MapPath(filepath));
                }

                mylist = document1.GetElementsByTagName(tagname);
            }
            catch
            {

            }
            return mylist;
        }

        /// <summary>
        /// 更新配置文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="element"></param>
        /// <param name="value"></param>
        public static void UpdateConfig(string filepath, string element, string nodename, string nodevalue)
        {

            try
            {
                XmlDocument document1 = new XmlDocument();
                string _filepath = string.Empty;
                if (HttpContext.Current != null)
                {
                    _filepath = HttpContext.Current.Server.MapPath(filepath);
                }
                
                document1.Load(_filepath);
                document1.GetElementsByTagName(element)[0].SelectSingleNode(nodename).InnerText = nodevalue;
                document1.Save(_filepath);

                //DefalutSkinIdInit();

            }
            catch
            {

            }
        }

        /// <summary>
        /// 系统基本配置
        /// </summary>
        public static Dictionary<string, string> baseConfig
        {
            get
            {
                if (_baseconfig == null)
                {
                    BaseConfigInit();
                }
                return _baseconfig;
            }
        }

        private static Dictionary<string, string> _baseconfig = null;
        public static string m_BaseConfigFilePath = "~/config/baseConfig.Config";                         //系统基本配置
    }
}
